package cn.kgc.springboot.vue.controller;

import cn.kgc.springboot.vue.commons.QueryCondition;
import cn.kgc.springboot.vue.entity.Role;
import cn.kgc.springboot.vue.exception.BusinessException;
import cn.kgc.springboot.vue.response.ResponseResult;
import cn.kgc.springboot.vue.service.RoleService;
import jakarta.annotation.Resource;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * @author 课工场
 * @date 2024/12/21
 * @description
 */
@RestController
@RequestMapping("role")
public class RoleController {

    @Resource
    private RoleService roleService;

    @GetMapping("list")
    public ResponseResult roleList() {
        List<Role> list = roleService.list();
        return ResponseResult.success().data("roleList", list);
    }

    @PostMapping("page")
    public ResponseResult rolePage(@RequestBody QueryCondition<Role> queryCondition) {

       return   roleService.rolePage(queryCondition);
    }


    @PostMapping("addOrUpdate")
    public ResponseResult addOrUpdateRole(@RequestBody Role role) {

        //  role 实体类中id属性不为null则更新对象    否则添加数据
        try {
            roleService.saveOrUpdate(role);
        }catch (DuplicateKeyException e){
            throw  new BusinessException("角色名或权限字符重复");
        }

        return  ResponseResult.success();
    }


    @DeleteMapping("delete/{id}")
    public ResponseResult deleteRole(@PathVariable Integer id){

        return  roleService.deleteRole(id);
    }


    @GetMapping("/rolePermissions/{id}")
    public ResponseResult rolePermissions(@PathVariable Integer id){
        return  roleService.rolePermissions(id);
    }


    // 添加角色权限信息
    @PostMapping("/addRolePermissions")
    public ResponseResult addRolePermissions(@RequestBody HashMap<String,Object> params){
        return  roleService.addRolePermissions(params);
    }



}
